home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / fortune / algorithm next >
Text File  |  1996-04-07  |  2KB  |  79 lines

  1. By popular demand, here's a brief explanation of how the fortune program
  2. works. It actually does a lot more than this, with regard to Workbench
  3. stuff, but this is the basics. The actual program is also appallingly
  4. written - it grew organically from a CLI only program.
  5.  
  6. File format
  7. -----------
  8.  
  9.     huffman compression table (see text.c)
  10.     long:       number of fortunes (called "num" hereafter).
  11.     num longs:  start position of each fortune text
  12.     num strings:the fortunes. The strings are null-terminated compressed
  13.                 strings.
  14.  
  15. Makefort Algorithm
  16. ------------------
  17.  
  18. open the input file
  19. clearhuffman()      (initialise the working table)
  20. count=0
  21. do
  22.     read a fortune, until you get an EOF or %%
  23.     addtohuffman(fortune) 
  24.         (this analyses the fortune for later compression)
  25.     add fortune to linked list of fortunes
  26.     increment count
  27. until End of File
  28. close the input file
  29.  
  30. genhuffman()    (generate the compression table)
  31. freehuffman()   (free the working table)
  32.  
  33. open the output file
  34. savehuffman(output file pointer)    (save the compression table)
  35. write count as a long integer to the output file
  36.  
  37. pos=4*(number of fortunes+1)+20*16
  38.     (this is the position of the first fortune text.
  39.     4*(number of fortunes+1) is the size of the jumptable and
  40.     fortune count, and 20*16 is the size of the compression table)
  41.  
  42. (... and write the jump table)
  43. for each fortune in the linked list do
  44.     write pos as a long to the output file
  45.     increment pos by the length of the compressed string, +1 for the NULL
  46.         (in C: pos+=strlen(compress(fortune->data))+1)
  47.     at the end.
  48. end
  49.  
  50. (finally write the fortunes)
  51. for each fortune in the linked list do
  52.     compress the fortune
  53.     write it out, followed by a NULL.
  54. end
  55. close the output file
  56.  
  57.  
  58.  
  59. The Readfort Algorithm
  60. ----------------------
  61.  
  62.     This is a simple version of the fortune cookie program- it only
  63. reads one fortune, and prints it in the CLI window.
  64.  
  65. open the input file
  66. loadhuffman(file pointer)   (read the compression table)
  67. read the number of fortunes, as a long
  68. fortune chosen=random number from 0 to number of fortunes-1
  69. seek to the position 4*(fortune chosen+1) + 20*16
  70. read a long integer - this is the position of the fortune text
  71. seek to position given by this integer
  72. read a null-terminated string
  73. uncompressed string=uncompress(string read in)
  74. print uncompressed string
  75.  
  76. Easy, huh?
  77.  
  78.  
  79.